﻿package com.zkingsoft.services.pc.impl;

import com.zkingsoft.services.pc.PcMicroElementService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.pc.PcMicroElement;
import com.zkingsoft.model.pc.PcMicroFood;
import com.zkingsoft.dao.pc.PcMicroElementDao;
import com.zkingsoft.dao.pc.PcMicroFoodDao;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-03-14 14:37
 */
@Service("pcMicroElementService")
public class  PcMicroElementServiceImpl implements    PcMicroElementService  {

	
	@Autowired
	private PcMicroElementDao pcMicroElementDao;
	@Autowired
	private PcMicroFoodDao pcMicroFoodDao;
	@Autowired
	private ServiceUtil serviceUtil;
	@Override
	public int add(PcMicroElement pcMicroElement){
		if (serviceUtil.addCheckRepeat("pc_micro_element", "mic_name", pcMicroElement.getMicName())) {
			throw new GlobleException("微量元素已经存在");
		}
		return pcMicroElementDao.insert(pcMicroElement);
		
	}
	
	@Override
	public int batchAdd(List<PcMicroElement>  pcMicroElementList) {
		int num = 0;

		int c = 10000;
		int size = pcMicroElementList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= pcMicroElementList.size() ? pcMicroElementList.size() : end;
			List<PcMicroElement> insertList = pcMicroElementList.subList(begin, end);
			num += pcMicroElementDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(PcMicroElement oldPcMicroElement
	,PcMicroElement newPcMicroElement){
	
		if (!ModelUtils.isModified(oldPcMicroElement, newPcMicroElement)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldPcMicroElement, newPcMicroElement);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("micId", oldPcMicroElement.getMicId());
			pcMicroElementDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(PcMicroElement pcMicroElement){
	
		return pcMicroElementDao.updateByModel(pcMicroElement);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
		PcMicroFood pcMicroFood=new PcMicroFood();
		for(Long id:list){
			pcMicroFood.setMicId(id);
			pcMicroFoodDao.deleteByModel(pcMicroFood);
		}
		return pcMicroElementDao.deleteByIds(list);
	}

	@Override
	public int removeById(Long micId){
	
		return pcMicroElementDao.deleteById(micId);
	
	}
	
	@Override
	public int removeByModel(PcMicroElement pcMicroElement){
	
		return pcMicroElementDao.deleteByModel(pcMicroElement);
	
	}
	
	
	@Override
	public List<PcMicroElement> findInPage(PcMicroElement pcMicroElement,  PaginationVO pageVo){
	
		return pcMicroElementDao.selectInPage(pcMicroElement , pageVo);
	
	}
	
	@Override
	public List<PcMicroElement> findByModel(PcMicroElement pcMicroElement){
	
		return pcMicroElementDao.selectByModel(pcMicroElement);
	
	}
	
	@Override
	public int  findTotal(PcMicroElement pcMicroElement){
	
		return pcMicroElementDao.selectTotalRecord(pcMicroElement);
	
	}
	
	@Override
	public PcMicroElement  findById(Long micId){
	
		return pcMicroElementDao.selectById(micId);
	
	}

	public List<PcMicroElement> selectMicroFood(Long fodId) {
		
		return pcMicroElementDao.selectMicroFood(fodId);
	}

	
	
	

   	
	
	
}